package com.ledi.guns.common.persistence.model.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * 文件名称： com.ledi.guns.common.persistence.model.vo.RoutesVo.java<br/>
 * 初始作者： 钟小平<br/>
 * 创建日期： 2018年3月19日<br/>
 * 功能说明： 前端路由菜单 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者 日期 修改内容<br/>
 * ================================================<br/>
 * Copyright (乐迪教育科技有限公司-幼儿事业部) 2017-2018.All rights reserved.<br/>
 */
public class RoutesVo implements Comparable, Serializable {

	/**
	 * 字段描述: [字段功能描述]
	 */
	private static final long serialVersionUID = -58529994504625227L;

	private Long id;

	/**
	 * 父节点
	 */
	private Long pId;

	/**
	 * 排序
	 */
	private Integer num;
	/**
	 * 路由名称
	 */
	private String name;
	/**
	 * 组件
	 */
	private String component;
	/**
	 * 路径
	 */
	private String path;
	/**
	 * 图标class
	 */
	private String iconCls;
	/**
	 * 叶子
	 */
	private String leaf;
	/**
	 * 是否隐藏
	 */
	private String hidden;
	/**
	 * 关键字
	 */
	private String key;

	/**
	 * 子对象
	 */
	private List<RoutesVo> children;

	/**
	 * 查询子节点时候的临时集合
	 */

	private List<RoutesVo> linkedList = new ArrayList<RoutesVo>();

	public Long getId() {

		return id;
	}

	public void setId(Long id) {

		this.id = id;
	}

	public String getName() {

		return name;
	}

	public void setName(String name) {

		this.name = name;
	}

	public String getComponent() {

		return component;
	}

	public void setComponent(String component) {

		this.component = component;
	}

	public String getPath() {

		return path;
	}

	public void setPath(String path) {

		this.path = path;
	}

	public String getIconCls() {

		return iconCls;
	}

	public void setIconCls(String iconCls) {

		this.iconCls = iconCls;
	}

	public String getLeaf() {

		return leaf;
	}

	public void setLeaf(String leaf) {

		this.leaf = leaf;
	}

	public String getHidden() {

		return hidden;
	}

	public void setHidden(String hidden) {

		this.hidden = hidden;
	}

	public String getKey() {

		return key;
	}

	public void setKey(String key) {

		this.key = key;
	}

	public Integer getNum() {

		return num;
	}

	public void setNum(Integer num) {

		this.num = num;
	}

	public Long getpId() {

		return pId;
	}

	public void setpId(Long pId) {

		this.pId = pId;
	}

	public List<RoutesVo> getChildren() {

		return children;
	}

	public void setChildren(List<RoutesVo> children) {

		this.children = children;
	}

	/**
	 * 构建菜单列表
	 *
	 * @date 2017年2月19日 下午11:18:19
	 */
	public static List<RoutesVo> buildTitle(List<RoutesVo> nodes) {

		new RoutesVo().buildNodeTree(nodes);
		List<RoutesVo> menuNodes = clearLevelTwo(nodes);
		// 对菜单排序
		Collections.sort(nodes);

		// 对菜单的子菜单进行排序
		for (RoutesVo menuNode : menuNodes) {
			if (menuNode.getChildren() != null && menuNode.getChildren().size() > 0) {
				Collections.sort(menuNode.getChildren());
			}
		}

		return menuNodes;
	}

	/**
	 * 清除所有二级菜单
	 *
	 * @date 2017年2月19日 下午11:18:19
	 */
	public static List<RoutesVo> clearLevelTwo(List<RoutesVo> nodes) {

		ArrayList<RoutesVo> results = new ArrayList<RoutesVo>();
		for (RoutesVo node : nodes) {
			Long levels = node.getpId();
			if (levels.equals(0L)) {
				results.add(node);
			}
		}
		return results;
	}

	private void buildNodeTree(List<RoutesVo> nodeList) {

		for (RoutesVo treeNode : nodeList) {
			List<RoutesVo> linkedList = treeNode.findChildNodes(nodeList, treeNode.getId());
			if (linkedList.size() > 0) {
				treeNode.setChildren(linkedList);
			}
		}

	}

	private List<RoutesVo> findChildNodes(List<RoutesVo> nodeList, Long parentId) {

		if (nodeList == null && parentId == null) {
			return null;
		}
		for (RoutesVo routesVo : nodeList) {
			RoutesVo node = routesVo;

			// 根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (node.getpId() != 0 && parentId.equals(node.getpId())) {
				recursionFn(nodeList, node, parentId);
			}
		}
		return linkedList;
	}

	/**
	 * 遍历一个节点的子节点
	 *
	 * @author fengshuonan
	 */
	public void recursionFn(List<RoutesVo> nodeList, RoutesVo node, Long pId) {

		List<RoutesVo> childList = getChildList(nodeList, node);// 得到子节点列表
		if (childList.size() > 0) {// 判断是否有子节点
			if (node.getpId().equals(pId)) {
				linkedList.add(node);
			}
			Iterator<RoutesVo> it = childList.iterator();
			while (it.hasNext()) {
				RoutesVo n = it.next();
				recursionFn(nodeList, n, pId);
			}
		} else {
			if (node.getpId().equals(pId)) {
				linkedList.add(node);
			}
		}
	}

	/**
	 * 得到子节点列表
	 *
	 * @author fengshuonan
	 */
	private List<RoutesVo> getChildList(List<RoutesVo> list, RoutesVo node) {

		List<RoutesVo> nodeList = new ArrayList<RoutesVo>();
		Iterator<RoutesVo> it = list.iterator();
		while (it.hasNext()) {
			RoutesVo n = it.next();
			if (n.getpId().equals(node.getId())) {
				nodeList.add(n);
			}
		}
		return nodeList;
	}

	@Override
	public int compareTo(Object o) {

		RoutesVo menuNode = (RoutesVo) o;
		Integer num = menuNode.getNum();
		if (num == null) {
			num = 0;
		}
		return this.num.compareTo(num);
	}

}
